package com.rs2hd.packethandler;

import com.rs2hd.Constants;
import com.rs2hd.event.CoordinateEvent;
import com.rs2hd.model.NPC;
import com.rs2hd.model.Player;
import com.rs2hd.model.World;
import com.rs2hd.net.Packet;
import org.apache.mina.common.IoSession;

public class NpcPacketHandler implements PacketHandler {

	@Override
	public void handlePacket(Player player, IoSession session, Packet packet) {
		switch (packet.getId()) {
		case 217:
			/*
			 * Option 1.
			 */
			option1(player, session, packet);
			break;
		case 38:
			/*
			 * Option 2.
			 */
			option2(player, session, packet);
			break;
		case 254:
			/*
			 * Option 3.
			 */
			option3(player, session, packet);
			break;
		case 207:
			/*
			 * Attack.
			 */
			attack(player, session, packet);
			break;
		}
	}

	private void attack(final Player player, IoSession session, Packet packet) {
		int id = packet.readLEShort();
		System.out.print(id);
		final NPC npc = World.getInstance().getNpcList().get(id);
		player.turnTemporarilyTo(npc);
		player.getWalkingQueue().GoTo(player, npc.getLocation().getX(), npc.getLocation().getY());
		if (player.getCombat().usingRange()) {
			player.getWalkingQueue().reset();
		
		}
		player.getCombat().enemyId = id;
		player.getCombat().attackNpc = true;
	}

	private void option1(final Player player, IoSession session, Packet packet) {
		int id = packet.readLEShortA() & 0xFFFF;
	System.out.println("ID: "+id);
        if (id < 0 || id >= Constants.NPC_CAP) {
            return;
        }
        final NPC npc = World.getInstance().getNpcList().get(id);
        if (npc == null) {
            return;
        }
        if (npc.getId() == 521 || npc.getId() == 520) {
	npc.turnTemporarilyTo(player);
	World.getInstance().getShopManager().openShop(player, 1);
        }
}
	private void option2(final Player player, IoSession session, Packet packet) {
		int id = packet.readInt() & 0xFFFF;
		System.out.println("ID: "+id);
		if (id < 0 || id >= Constants.NPC_CAP) {
			return;
		}
				final NPC npc = World.getInstance().getNpcList().get(id);
		player.turnTemporarilyTo(npc);
		player.getWalkingQueue().GoTo(player, npc.getLocation().getX(), npc.getLocation().getY());
		if (npc == null) {
			return;
		}
		player.turnTo(npc);
		World.getInstance().registerCoordinateEvent(new CoordinateEvent(player, npc.getLocation(), npc.getDefinition().getSize()) {
			@Override
			public void run() {
				player.resetTurnTo();
				if (npc.getId() == 521 || npc.getId() == 520) {
					npc.turnTemporarilyTo(player);
					World.getInstance().getShopManager().openShop(player, 1);
				}
			}
		});
	}

	private void option3(final Player player, IoSession session, Packet packet) {
		int id = packet.readLEShort() & 0xFFFF;
		System.out.println("ID: "+id);
		if (id < 0 || id >= Constants.NPC_CAP) {
			return;
		}
		final NPC npc = World.getInstance().getNpcList().get(id);
		if (npc == null) {
			return;
		}
		player.turnTo(npc);
		World.getInstance().registerCoordinateEvent(new CoordinateEvent(player, npc.getLocation(), npc.getDefinition().getSize()) {
			@Override
			public void run() {
				player.resetTurnTo();
			}
		});
	}

}
